/**
 * Created by Administrator on 15-10-20.
 */
var Core = require("./core.js");
var core = new Core();
var Product = RichBase.extend({
	statics : {
		cacheData : null
	},
	EVENTS : {

	},
	//当产品数大于50个时，开启搜索功能(admin默认开启搜索)
	PRODUCT_COUNT_TRIGGER_SEARCH : 50,
	cacheData : null,
	init : function(opt){
		this.container = opt.container;
		this.listUl = this.container.find(".listUl");
		this.searchInp = this.container.find(".searchInp");
		var that = this;
		var sid = $("#gsid").val() || "";
		//加载产品
		core.fetchProduct({
			keyword : "",
			sid : sid,
			loading : function(){
				that.showProdStatus();
				$("#prodBoxStatus").html('<img class="loading" src="'+core.LOADING_IMG+'"/><span class="t">正在加载产品...</span>');
			},
			removeLoaidng : function(){ alert(1); that.hideProdStatus() },
			timeout : function(res){
				that.showProdStatus();
				$("#prodBoxStatus").show().html('<span class="t">加载产品超时，请稍后重试...</span>');
			},
			serverError : function(res){
				that.showProdStatus();
				$("#prodBoxStatus").show().html('<span class="t">加载产品出错，请稍后重试...</span>');
			},
			empty : function(res){
				that.showProdStatus();
				$("#prodBoxStatus").show().html('<span class="t">没有产品...</span>');
			},
			success : function(res){
				var list = res.list;
				that.statics.cacheData = list;
				that.listUl.html(that.buildProductUl(list));
			}
		});
	},
	showProdStatus : function(){
		$("#prodPhoto").hide();
		$("#prodMsg").hide();
		$("#prodBoxStatus").show();
	},
	hideProdStatus : function(){
		$("#prodPhoto").show();
		$("#prodMsg").show();
		$("#prodBoxStatus").hide();
	},
	buildProductUl : function(data){
		var html = "";
		for(var i in data){
			var d = data[i];
			var id = d["id"];
			var salerid = d["salerid"];
			var terminal = d["terminal"];
			var imgpath = d["imgpath"];
			var title = d["title"] || "无标题产品";
			html += '<li class="pItem" data-id="'+id+'" data-salerid="'+salerid+'" data-terminal="'+terminal+'" data-img="'+imgpath+'">'+title+'</li>';
		}
		return html;
	}
});
module.exports = Product;